package com.Nuyoah.demo01.controller;

import com.Nuyoah.demo01.DTO.CommunityDTO;
import com.Nuyoah.demo01.DTO.CommunityPageQry;
import com.Nuyoah.demo01.DTO.CommunityQry;
import com.Nuyoah.demo01.common.JsonResult;
import com.Nuyoah.demo01.entity.Community;
import com.Nuyoah.demo01.service.CommunityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/community")
public class CommunityController {

    @Resource
    private CommunityService communityService;

    //创建社区
    @PostMapping("/createCommunity")
    public JsonResult<String> createCommunity(@RequestBody CommunityDTO dto) {
        //dto为空则返回错误
        if (dto == null) {
            return new JsonResult<>("400", "不可传空值");
        }
        try {
            communityService.createCommunity(dto);
        } catch (Exception e) {
            // 捕获异常，返回错误信息
            e.printStackTrace(); // 打印异常堆栈信息，便于调试
            return new JsonResult<>("1", "创建失败：" + e.getMessage());
        }
        return new JsonResult<>("200", "创建成功");
    }

    //根据id删除单个社区
    @PostMapping("/deleteCommunity")
    public JsonResult<String> deleteCommunity(@RequestParam Integer communityId ) {
        //dto为空则返回错误
        if (communityId == null) {
            return new JsonResult<>("400", "不可传空值");
        }
        try {
            communityService.deleteCommunity(communityId);
        } catch (Exception e) {
            // 捕获异常，返回错误信息
            e.printStackTrace(); // 打印异常堆栈信息，便于调试
            return new JsonResult<>("1", "创建失败：" + e.getMessage());
        }
        return new JsonResult<>("200", "创建成功");
    }
        //修改社区信息
    @PostMapping("/updateCommunity")
    public JsonResult<String> updateCommunity(@RequestBody CommunityQry qry) {
        if (qry != null){
            if (communityService.updateCommunity(qry)>0){
                return new JsonResult<>("200", "修改成功");
            }
        }else {
            return new JsonResult<>("400", "不可传空值");
        }
        return new JsonResult<>("1", "修改失败");
    }

        //分页查询社区信息
    @PostMapping("/selectPageCommunity")
    public JsonResult<List<Community>> selectPageCommunity(@RequestBody @Valid CommunityPageQry qry) {
        return new JsonResult<>("200",communityService.selectPageCommunity(qry), "查询成功");
    }

        //查询全部社区信息
    @PostMapping("/selectAllCommunity")
    public JsonResult<List<Community>> selectAllCommunity() {
        return new JsonResult<>("200",communityService.selectAllCommunity(), "查询成功");
    }

        //查询单个社区信息
    @PostMapping("/selectCommunityById")
    public JsonResult<Community> selectCommunityById(@RequestParam Integer communityId) {
        return new JsonResult<>("200",communityService.selectCommunityById(communityId), "查询成功");
    }

        //查询某个用户创建的社区信息
    @PostMapping("/selectCommunityByUserId")
    public JsonResult<List<Community>> selectCommunityByUserId(@RequestParam Integer userId) {
        return new JsonResult<>("200",communityService.selectCommunityByUserId(userId), "查询成功");
    }

}
